//
//  SelectionHeaderView.swift
//  HuaYun
//
//  Created by enghou on 2018/12/11.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class SelectionHeaderView: UIView {

    @IBOutlet weak var stack: UIStackView!
    @IBOutlet weak var orgView: UIView!
    @IBOutlet weak var beginView: UIView!
    @IBOutlet weak var endView: UIView!
    @IBOutlet weak var searchView: UIView!
    @IBOutlet weak var clearView: UIView!
    
    func endedit()->Void{
        self.endEditing(true)
//        self.beginDateShadowField.resignFirstResponder()
//        self.endDateShadowField.resignFirstResponder()
    }
    
    private var _orgNotNeeded: Bool = false
    
    var orgNotNeeded: Bool {
        get{
            return _orgNotNeeded
        }
        
        set{
            _endNotNeeded = newValue
            if newValue == true {
                if self.stack.arrangedSubviews.contains(self.orgView) {
                    self.orgView.removeFromSuperview()
                }
            } else {
                if !self.stack.arrangedSubviews.contains(self.orgView) {
                    self.stack.addArrangedSubview(self.orgView)
                }
            }
        }
    }
    
    private var _endNotNeeded = false
    var endNotNeeded: Bool {
        get{
            return self._endNotNeeded
        }
        set{
            _endNotNeeded = newValue
            if newValue == true {
                if self.stack.arrangedSubviews.contains(self.endView) {
                    self.endView.removeFromSuperview()
                }
            } else {
                if !self.stack.arrangedSubviews.contains(self.endView) {
                    self.stack.addArrangedSubview(self.endView)
                }
            }
        }
    }
    
    var firstresponder: UITextField? {
        get{
            if self.beginDateShadowField.isFirstResponder {
                return self.beginDateField
            } else if self.endDateShadowField.isFirstResponder {
                return self.endDateField
            }
            return nil
        }
    }
    
    var currentpicker: UIView? {
        get{
            if self.beginDateShadowField.isFirstResponder {
                return self.beginDateShadowField.inputView
            } else if self.endDateShadowField.isFirstResponder {
                return self.endDateShadowField.inputView
            }
            return nil
        }
    }
    
    @IBOutlet weak var orgField: UITextField!
    
    @IBOutlet private weak var orgShadowField: UITextField!
    
    @IBOutlet weak var beginDateField: UITextField!
    
    @IBOutlet private weak var beginDateShadowField: UITextField!
    
    @IBOutlet weak var endDateField: UITextField!
    
    @IBOutlet weak var endDateShadowField: UITextField!
    
    @IBOutlet weak var orgButton: UIButton!
    
    @IBOutlet weak var searchButton: UIButton!
    
    @IBOutlet weak var clearButton: UIButton!
    
    @IBAction func orgAction(_ sender: Any) {
        self.orgShadowField.becomeFirstResponder()
    }
    
    @IBAction func beginDateInputAction(_ sender: Any) {
        
        self.beginDateShadowField.becomeFirstResponder()
    }
    
    @IBAction func endDateInputAction(_ sender: Any) {
        
        self.endDateShadowField.becomeFirstResponder()
    }
    
    private var _delegate: RefreshedTableViewController?
    var delegate: RefreshedTableViewController?{
        set{
            _delegate = newValue
            guard _delegate != nil else {
                return
            }
            beginDateShadowField.inputView = _delegate?.datePicker
            beginDateShadowField.inputAccessoryView = _delegate?.toolbarview
            endDateShadowField.inputView = _delegate?.endPicker
            endDateShadowField.inputAccessoryView = _delegate?.toolbarview
            orgButton.addTarget(_delegate, action: #selector(_delegate!.chooseorg), for: .touchUpInside)
            searchButton.addTarget(_delegate, action: #selector(_delegate!.searchAction), for: .touchUpInside)
            clearButton.addTarget(_delegate, action: #selector(_delegate!.clear), for: .touchUpInside)
        }
        get{
            return _delegate
        }
    }
}
